// Xampler, a MIDI-driven sampler
// Copyright (C) 2006 Nick Thomas
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
// 02110-1301 USA.

#include "InstrumentEditor.hpp"
#include "State.hpp"
using namespace xampler;

InstrumentEditor::InstrumentEditor()
{
    for (int i = 0; i < MAX_SAMPLES; i++) {
        m_sample_list.pack_start(m_sample_strips[i], Gtk::PACK_SHRINK);
    }

    m_sample_list_scrollbar.set_shadow_type(Gtk::SHADOW_NONE);
    m_sample_list_scrollbar.set_policy(Gtk::POLICY_NEVER,
                                       Gtk::POLICY_AUTOMATIC);
    m_sample_list_scrollbar.add(m_sample_list);

    pack_start(m_sample_list_scrollbar, Gtk::PACK_EXPAND_WIDGET);
}

void InstrumentEditor::set_current_instrument(int current_instrument)
{
    m_current_instrument = current_instrument;
    Instrument &instrument = g_state->get_instrument(m_current_instrument);

    for (int i = 0; i < MAX_SAMPLES; i++) {
        m_sample_strips[i].set_sample(instrument.get_sample(i));
    }
}

void InstrumentEditor::handle_synth_message(SynthMessage &msg)
{
    switch (msg.type) {
    case SYNTH_MSG_SAMPLE_ON:
        if (msg.sample_on.instrument == m_current_instrument) {
            m_sample_strips[msg.sample_on.sample].increment_playing_count();
        }
        break;
    case SYNTH_MSG_SAMPLE_OFF:
        if (msg.sample_off.instrument == m_current_instrument) {
            m_sample_strips[msg.sample_off.sample].decrement_playing_count();
        }
    }
}
